﻿package plugin.instances.game
{
    import cmd.*;
    import com.core.*;
    import com.sh.game.util.*;
    import consts.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.instance.*;
    import plugin.instances.*;
    import uiresource.instances.ui.*;
    import view.*;
    import view.message.*;
    import view.mornExtends.*;

    public class EmsGame extends EmsUI implements IInstance
    {
        private var maxBlood:int = 0;
        private var isFucking:Boolean = false;
        private var showRec:Rectangle;
        private var lastBlood:int = 0;
        private var _sendTime:Number = 0;
        private var _intvalid2:uint = 0;
        private var emsObj:Object;
        private var _intvalid:int = 0;
        private var _lastTime:int = 0;
        private var _endtime:int = 0;

        public function EmsGame()
        {
            this.showRec = new Rectangle(0, 0, 250, 9);
            return;
        }// end function

        private function doAuto(event:MouseEvent) : void
        {
            ModelProxy.change("data", "isAutoEms", true);
            ModelProxy.change("zone", "emsAuto", true);
            ModelProxy.change("guide", "hide", GuideType.GUIDE_AUTOEMS);
            return;
        }// end function

        private function timerCounter() : void
        {
            var _loc_1:* = new Date().getTime();
            var _loc_2:* = _loc_1 - this._sendTime;
            var _loc_3:* = int((15000 - _loc_2) / 1000 + 1);
            if (_loc_3 < 0)
            {
                clearInterval(this._intvalid2);
                this._intvalid2 = 0;
            }
            return;
        }// end function

        protected function dotrans(event:MouseEvent) : void
        {
            var event:* = event;
            Alert2.show("传送至镖车处需要消耗10礼券，确定要传送吗？", "", ["确定", "取消"], function (param1:int, param2:Object) : void
            {
                var _loc_3:int = 0;
                if (param1 == 0)
                {
                    _loc_3 = ModelProxy.getValue("data", "bag.bonus");
                    if (_loc_3 >= 10)
                    {
                        ModelProxy.change("plugin", "sendSysMsg", {name:"ems", param:null, cmd:CMDEms.EMSSend});
                    }
                    else
                    {
                        ModelProxy.change("movetips", "error", "礼券不足");
                    }
                }
                return;
            }// end function
            , null, true, true, false);
            return;
        }// end function

        public function initShow(param1:Object, param2:Dictionary) : void
        {
            this.show(param1, param2);
            return;
        }// end function

        public function show(param1:Object, param2:Dictionary) : void
        {
            var _loc_6:int = 0;
            var _loc_8:Number = NaN;
            var _loc_3:* = ModelProxy.getValue("data", "ems");
            var _loc_4:* = ConfigDictionary.data.ems;
            var _loc_5:* = ConfigDictionary.data.monster;
            this.emsObj = _loc_4[_loc_3[0]];
            this.maxBlood = _loc_5[this.emsObj.mid].hp;
            var _loc_7:* = param2[InstancesPublic.EMSFUCKINGSTATE];
            if (param2[InstancesPublic.EMSFUCKINGSTATE] != null)
            {
                _loc_6 = _loc_7.hp;
            }
            else
            {
                _loc_6 = this.maxBlood;
            }
            this.lastBlood = _loc_6;
            txt_num.text = _loc_6 + "/" + this.maxBlood;
            if (this.maxBlood == 0)
            {
                this.showRec.width = 0;
                img_cd.scrollRect = this.showRec;
            }
            else
            {
                _loc_8 = _loc_6 / this.maxBlood;
                if (_loc_8 > 0.7)
                {
                    img_cd.url = "png.common.boss03";
                }
                else if (_loc_8 > 0.3)
                {
                    img_cd.url = "png.common.boss04";
                }
                else
                {
                    img_cd.url = "png.common.red";
                }
                this.showRec.width = 230 * _loc_8;
                img_cd.scrollRect = this.showRec;
            }
            txt_state.text = "移动";
            this._endtime = _loc_3[2];
            this.lasttimeCount();
            shuoming.toolTip = "说明：\n1.只有在押送犯人周围，犯人才会前进\n2.押送的犯人被攻击后不会停止\n3.犯人被打死会导致押送失败，获得部分奖励\n4.放弃押镖没有任何奖励\n5.押送成功后，可以多倍领取奖励\n6.劫杀别人的囚犯获得大量金币\nPS:当你被攻击时，你可以向行会或是好友求助";
            this.showItem("1/4");
            return;
        }// end function

        private function showItem(param1:String) : void
        {
            var _loc_2:* = new ItemSplit();
            var _loc_3:* = _loc_2.doSplits(param1, "&", "/", "#");
            switch(_loc_3[0].length)
            {
                case 1:
                {
                    list.x = 120;
                    break;
                }
                case 2:
                {
                    list.x = 85;
                    break;
                }
                case 3:
                {
                    list.x = 50;
                    break;
                }
                default:
                {
                    break;
                }
            }
            list.array = _loc_3[0];
            list.renderHandler = new Handler(this.itemRender, [list]);
            return;
        }// end function

        private function itemRender(param1:List, param2:Component, param3:int) : void
        {
            if (param3 < param1.length)
            {
                (param2 as FlashItem).data = param1.array[param3];
            }
            return;
        }// end function

        public function lasttimeCount() : void
        {
            if (this._intvalid > 0)
            {
                SecondTimerUtil.instance.removeTask(this._intvalid);
            }
            var _loc_1:* = int((ModelProxy.getValue("data", "servertime") + getTimer()) / 1000);
            this._lastTime = this._endtime - _loc_1;
            txt_time.text = TimeDataFormatUtil.getLeftTime(this._lastTime);
            this._intvalid = SecondTimerUtil.instance.addSecondTask(this.timerHandler);
            return;
        }// end function

        private function timerHandler() : void
        {
            var _loc_1:* = int((ModelProxy.getValue("data", "servertime") + getTimer()) / 1000);
            this._lastTime = this._endtime - _loc_1;
            if (this._lastTime > 0)
            {
                txt_time.text = TimeDataFormatUtil.getLeftTime(this._lastTime);
            }
            else if (this._intvalid > 0)
            {
                SecondTimerUtil.instance.removeTask(this._intvalid);
                ModelProxy.change("plugin", "sendSysMsg", {name:"ems", param:null, cmd:7});
            }
            return;
        }// end function

        public function doLink(event:TextEvent) : void
        {
            ModelProxy.change("data", "taskfindpath", this.emsObj.tonpc);
            var _loc_2:* = this.emsObj.tnpcstation.split("#");
            ModelProxy.change("zone", "stopGuaji", null);
            ModelProxy.change("zone", "autoMove", {x:_loc_2[1], y:_loc_2[2], tostageid:_loc_2[0], onhorse:true});
            return;
        }// end function

        public function cannel(event:MouseEvent = null) : void
        {
            var npc:Object;
            var e:* = event;
            if (txt_oper.label == "领取奖励")
            {
                npc = ConfigDictionary.data.sysnpc[145];
                ModelProxy.change("zone", "autoMove", {x:npc.x, y:npc.y, tostageid:npc.stageid, onhorse:false});
                ModelProxy.change("data", "taskfindpath", npc.npcid);
            }
            else
            {
                Alert2.show("您是否要放弃押运，确定后消耗押运次数，无任何奖励？", "", ["确定", "取消"], function (param1:int, param2:Object) : void
            {
                if (param1 == 0)
                {
                    ModelProxy.change("plugin", "sendSysMsg", {name:"ems", param:null, cmd:5});
                }
                return;
            }// end function
            , null, true);
            }
            return;
        }// end function

        public function updateShow(param1:int, param2:Object) : void
        {
            var _loc_3:int = 0;
            var _loc_4:Number = NaN;
            switch(param1)
            {
                case InstancesPublic.EMSFINISHED:
                {
                    txt_oper.label = "领取奖励";
                    break;
                }
                case InstancesPublic.EMSFUCKINGSTATE:
                {
                    _loc_3 = param2.hp;
                    this.lastBlood = _loc_3;
                    txt_num.text = _loc_3 + "/" + this.maxBlood;
                    if (this.maxBlood == 0)
                    {
                        this.showRec.width = 0;
                        img_cd.scrollRect = this.showRec;
                    }
                    else
                    {
                        _loc_4 = _loc_3 / this.maxBlood;
                        if (_loc_4 > 0.7)
                        {
                            img_cd.url = "png.common.boss03";
                        }
                        else if (_loc_4 > 0.3)
                        {
                            img_cd.url = "png.common.boss04";
                        }
                        else
                        {
                            img_cd.url = "png.common.red";
                        }
                        this.showRec.width = 230 * _loc_4;
                        img_cd.scrollRect = this.showRec;
                    }
                    switch(int(param2.state))
                    {
                        case 0:
                        {
                            txt_state.text = "移动";
                            break;
                        }
                        case 1:
                        {
                            txt_state.text = "停止";
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function destroyShow(param1:Dictionary) : void
        {
            ModelProxy.change("data", "emsIsFucking", false);
            param1[InstancesPublic.EMSFUCKINGSTATE] = {hp:this.lastBlood, state:0};
            return;
        }// end function

        public function initEvent() : void
        {
            btn_trans.addEventListener(MouseEvent.CLICK, this.dotrans);
            btn_auto.addEventListener(MouseEvent.CLICK, this.doAuto);
            txt_oper.clickHandler = new Handler(this.cannel);
            ModelProxy.change("guideitem", "guideid" + GuideType.GUIDE_AUTOEMS, btn_auto);
            GuideView.getInstance().doGuide(GuideType.GUIDE_AUTOEMS);
            return;
        }// end function

        public function removeEvent() : void
        {
            return;
        }// end function

    }
}
